package com.mall.dao;

import com.mall.domain.Member;
import com.mall.domain.Page;
import com.mall.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 会员的持久化层接口
 * @author : liangY
 * @date : 2020-12-30 11:25
 */
@Repository
public interface IMemberDao {

    /**
     * 查询所有会员
     * @param page 分页实体类
     * @return
     */
    @Select("select * from tb_member where username like #{page.query} limit #{page.num},#{page.size}")
    List<Member> findAll(@Param("page")Page page);


    /**
     * 验证登录
     */
    @Results(id = "loginMap")
    @Select("select * from tb_member where username=#{loginUser.username} and password=#{loginUser.password} and status=1")
    Member login(@Param("loginUser") Member loginUser);
    /**
     * 查询所有会员数量
     * @return
     */
    @Select("select count(*) from tb_member")
    Integer findTotal();

    /**
     * 更新会员
     * @param member 会员实体类
     * @return
     */
    @Update("update tb_member set username=#{username},password=#{password},nickname=#{nickname}," +
            "tel=#{tel},email=#{email},sex=#{sex},address=#{address},file=#{file},description=#{description}," +
            "integral=#{integral},status=#{status},updated=#{updated} where id=#{id}")
    Integer update(Member member);

    @Update("update tb_member set username=#{username},password=#{password},nickname=#{nickname}," +
            "tel=#{tel},email=#{email},sex=#{sex},address=#{address},file=#{file},description=#{description}," +
            "integral=#{integral} where id=#{id}")
    Integer update2(Member member);
    /**
     * 添加会员
     * @param member 会员实体类
     * @return
     */
    @Insert("insert into tb_member values(#{id},#{username},#{password},#{nickname},#{tel},#{email},#{sex}," +
            "#{address},#{file},#{description},#{integral},#{status},#{created},#{updated})")
    @SelectKey(keyColumn = "id", keyProperty = "id", resultType = Integer.class,
            before = false, statement = {"select last_insert_id()"})
    Integer add(Member member);

    /**
     * 删除会员
     * @param id 会员编号
     * @return
     */
    @Delete("delete from tb_member where id = #{id}")
    Integer del(Integer id);

}
